﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebSite_m_data_UserDetail : System.Web.UI.Page
{
    string userid;
    string sqlString;
    protected void Page_Load(object sender, EventArgs e)
    {

        WarnLabel.Visible = false;
        if (Session["UserId"] == null || Session["UserIdentity"].ToString() != "2")
        {
            Response.Redirect("Default.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
        else userid = Session["UserId"].ToString();

        return;
    }
    protected void InsertBtn_Click(object sender, EventArgs e)
    {
        try{
            //获取新加注册用户信息
            string userid = UserIdTextBox.Text;
            if(userid=="")
            {
                WarnLabel.Visible = true;
                WarnLabel.Text = "必须输入用户ID";
                return;
            }
            string password = UserpwdTextBox.Text;
            string name = NameTextBox.Text;
            string sex = SexDropDownList.SelectedItem.Value;
            string borndate = BornDateTextBox.Text;
            string location = CityDropDownList.SelectedItem.Text;
            string nationality = NationalityDropDownList.SelectedItem.Text;
            string identity = UserIdentityDropDownList.SelectedItem.Value;
            string grade = GradeDropDownList.SelectedItem.Text;
            string collegename = CollegeDropDownList.SelectedItem.Text;

            //将用户信息添加到数据库中。
            SqlConnection conn = new SqlConnection("server=localhost;Integrated Security=SSPI;Initial Catalog=CSHDB");
            sqlString = "select userid from UserInfo where UserId='" + userid+"'";
            conn.Open();
            SqlCommand command = new SqlCommand(sqlString, conn);
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                WarnLabel.Visible = true;
                WarnLabel.Text = "该ID用户已经注册！";
                return ;
            }
            //插入数据到UserInfo中
            reader.Dispose();
            sqlString="insert UserInfo(UserId,Password,UserIdentity,CountNO) values('"+userid+"',"+password+","+identity+",0)";
            command.CommandText=sqlString;
            command.ExecuteNonQuery();

            //插入数据到UserDetail表中
            sqlString="insert UserDetail(UserId,Name,BornDate,Location,Nationality,CollegeName,Sex,Grade) values('"+userid+
                "','"+name+"','"+borndate+"','"+location+"','"+nationality+"','"+collegename+"',"+sex+","+grade+")";
            command.CommandText=sqlString;
            command.ExecuteNonQuery();
            conn.Close();
            UserIdTextBox.Text="";
            UserpwdTextBox.Text="";
            NameTextBox.Text="";
            BornDateTextBox.Text="";
            WarnLabel.Visible = true;
            WarnLabel.Text = "成功注册用户" + userid;
        }
        catch(Exception ex)
        {
            WarnLabel.Text=ex.Message.ToLower();
            //跳转到错误处理页面。
            Session["ErrorMessage"] = ex.Message+"\nSQL:"+sqlString;
            Session["AtPage"] = "m_data_UserDetail.aspx";
            Response.Redirect("m_error.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
    }
}
